LOCALDIR:=.
BASEDIR:=.
SYSTEM:=$(shell uname)
MODULES:=

include $(LOCALDIR)/module.def

# Each module will add its source file to the following
# variables.
DEBUG_OBJS:=
RELEASE_OBJS:=

DEBUG_ARCHIVES:=
RELEASE_ARCHIVES:=

DEBUG_LIBS:=
RELEASE_LIBS:=

DEBUG_TARGETS:=
RELEASE_TARGETS:=

# Installing dirs
USRBIN:= /usr/sbin
USRLIB:= /usr/lib
INITD:= /etc/init.d
RC2D:= /etc/rc2.d

include $(patsubst %, $(BASEDIR)/%/module.mk, $(MODULES))

###############################################################################
# Targets
###############################################################################

default: 
	@echo
	@echo "Please use one of the following and available parameters:"
	@echo " * 'debug' to generate the libraries and applications with debug symbols and debug log capability."
	@echo " * 'release' to generate the libraries and applications without debug symbols (stripped) and no debug log capability."
	@echo " * 'clean' to clean all the objects, libraries, archives and generated binaries."
	@echo " * 'cleanall' to clean all generated files (as above) plus the dependency files."
	@echo " * 'install' to install the files into $(PREFIX) to be delivered together with GisusDLL library."
	@echo

debug: $(DEBUG_OBJS) $(DEBUG_LIBS) $(DEBUG_TARGETS)

release: $(RELEASE_OBJS) $(RELEASE_ARCHIVES) $(RELEASE_LIBS) $(RELEASE_TARGETS)

clean:
	@echo "Cleaning ..."
	@for m in $(MODULES) ; do find $(BASEDIR)/$$m/obj -type f -exec rm -f {} \; -print ; done
	@if [ -d lib ]; then find lib -type f -exec rm -f {} \; -print ; fi
	@if [ -d bin ]; then find bin -type f -exec rm -f {} \; -print ; fi
	@find . -mindepth 1 \( -name \*~ -o -name core\.\* \) -exec rm -Rf {} \; -print || true

cleanall:
	@echo "Cleaning all ..."
	@for m in $(MODULES) ; do find $(BASEDIR)/$$m/obj -type f -exec rm -f {} \; -print ; done
	@if [ -d lib ]; then find lib -type f -exec rm -f {} \; -print ; fi
	@if [ -d bin ]; then find bin -type f -exec rm -f {} \; -print ; fi
	@find . -mindepth 1 \( -name \*~ -o -name core\.\* \) -exec rm -Rf {} \; -print || true
	@for d in */dep; do find $$d -type f -exec rm -Rf {} \; -print; done 

install: 
	@for i in $(shell ls ./lib/release/*.so); do echo "Copying $$i into $(USRLIB) ..."; cp -f $$i $(USRLIB); done
	@for i in $(shell ls ./bin/release/*); do echo "Copying $$i into $(USRBIN) ..."; cp -f $$i $(USRBIN); done
	@echo "Creatting $(INITD)/bridge ..."
	@cp -f ./etc/bridge.sh $(INITD)/bridge
	@update-rc.d -f bridge start 80 2 3 4 5 . stop 30 0 1 6 .
	@$(RC2D)/S80bridge restart
	@for i in $(shell seq 1 1 7); do echo "Creatting $(INITD)/attacker$$i ..."; sed "s/attacker/attacker$$i/g" ./etc/attacker > $(INITD)/attacker$$i; chmod +x $(INITD)/attacker$$i; update-rc.d -f attacker$$i start 90 2 3 4 5 . stop 30 0 1 6 . ; $(RC2D)/S90attacker$$i restart; done

###############################################################################
# Rules
###############################################################################

.PRECIOUS: $(MYCRYPTO_BASEDIR)/obj/debug/%.s $(MYCRYPTO_BASEDIR)/obj/release/%.s 


# Includes the rules to build objects from inserted modules.
include $(patsubst %, $(BASEDIR)/%/rules.mk, $(MODULES))

# Includes the C/C++ header dependencies for each object (automatically generated)
#
# NOTE: The following supposes that the corresponding rules.mk file contains
# sufficient information to build the .d file.
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),cleanall)
ifneq ($(MAKECMDGOALS),doc)
ifneq ($(MAKECMDGOALS),delivery)
ifneq ($(MAKECMDGOALS),)
     include $(DEPS)
endif
endif
endif
endif
endif
